package com.example.utils;

import java.util.List;

public class ListPageHelper {
    public static List partition(List list, Integer pageNumber, final Integer pageSize){

        if (list == null) {
            return null;
        }

        if (list.size() == 0) {
            return null;
        }

        if(pageNumber<1){
            pageNumber = 1;
        }
        //计算总共有多少页
        Integer count = list.size();
        Integer pageCount;
        if(count % pageSize == 0){
            pageCount = count / pageSize;
        }else {
            pageCount = count / pageSize + 1;
        }

        //计算索引页,从哪里开始找,找第几页
        Integer fromIndex;
        Integer toIndex;
        if(!pageNumber.equals(pageCount)){
            fromIndex = (pageNumber - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        }else {
            fromIndex = (pageNumber - 1) * pageSize;
            toIndex = count;
        }

        List pageList = list.subList(fromIndex, toIndex);

        return pageList;
    }
}
